//
//  ASMakeAvatarSelectView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/28.
//

#import "ASMakeAvatarSelectView.h"
#import "TZImagePickerController.h"
@interface TZImagePickerController () <TZImagePickerControllerDelegate>

@end

@implementation ASMakeAvatarSelectView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.addImage];
        [self addSubview:self.addLab];
        [self addSubview:self.mainImage];
        [self addSubview:self.mainBtn];
        [self setAllViewLayout];
    }
    return self;
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.addImage mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.mas_centerY).mas_offset(-6);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }]; 
    
    [self.addLab mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.addImage.mas_bottom).mas_offset(9);
    }];
    
    [self.mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.mainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (UIImageView *)mainImage{
    if (!_mainImage) {
        _mainImage = [UIImageView new];
        _mainImage.contentMode = UIViewContentModeScaleAspectFill;
        _mainImage.clipsToBounds = YES;
    }
    return _mainImage;
}

- (UIImageView *)addImage{
    if (!_addImage) {
        _addImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover_tianjia"]];
    }
    return _addImage;
}

- (UILabel *)addLab{
    if (!_addLab) {
        _addLab = [UILabel new];
        _addLab.font = [UIFont systemFontOfSize:14];
        _addLab.textColor = [UIColor colorWithHexString:@"#1A1A1A"];
        _addLab.textAlignment = NSTextAlignmentCenter;
        _addLab.text = @"添加图片";
    }
    return _addLab;
}

- (UIButton *)mainBtn{
    if (!_mainBtn) {
        _mainBtn = [UIButton new];
        [_mainBtn addTarget:self action:@selector(clickMainBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _mainBtn;
}

- (void)clickMainBtn{
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingGif = NO;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.sortAscendingByModificationDate = NO;
    imagePickerVc.showSelectBtn = NO;
    imagePickerVc.allowCrop = YES;
    imagePickerVc.maxImagesCount = 1;
    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    imagePickerVc.cropRect = CGRectMake(0, (SCREENH_HEIGHT - SCREEN_WIDTH)/2, SCREEN_WIDTH, SCREEN_WIDTH);
    [currViewController() presentViewController:imagePickerVc animated:YES completion:nil];
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    if (photos.count > 0) {
        UIImage *image = photos[0];
        [self.mainImage setImage:image];
        self.showImage = image;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
